//
//  WmcSelectAddressCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/9.
//

import UIKit

class WmcSelectAddressCell: WmcBaseTableCell {
    // Internal

    /// 设置/取消常用地址
    var setCommonlyAddress: ((Bool, Int) -> Void)?
    
    /// 删除自定义地址
    var delCustomizedAddress: ((Int) -> Void)?

    override func makeUI() {
        contentView.addSubview(addressTitleLabel)
        contentView.addSubview(commonlyIconView)
        contentView.addSubview(addressDetailLabel)
        contentView.addSubview(commonlyBtn)
        contentView.addSubview(line)
        commonlyIconView.addSubview(commonlyLabel)

        addressTitleLabel.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.right.equalTo(-90)
        }

        commonlyIconView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalTo(addressDetailLabel.snp.centerY)
            make.width.equalTo(46)
            make.height.equalTo(16)
        }

        commonlyLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }

        addressDetailLabel.snp.makeConstraints { make in
            make.left.equalTo(64)
            make.bottom.equalTo(-16)
            make.right.equalTo(-90)
        }

        commonlyBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
            make.width.equalTo(64)
            make.height.equalTo(32)
        }

        line.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
        }

        commonlyBtn.addTarget(self, action: #selector(commonlyAction(_:)), for: .touchUpInside)
    }

    func bindModel(_ model: WmcSelectAddressModel, index: Int) {
        self.model = model
        self.index = index
        
        self.addressTitleLabel.text = model.poi.name
        self.addressDetailLabel.text = "\(model.poi.distance)米，\(model.poi.address ?? "")"
        self.addressDetailLabel.isHidden = model.style == .customized
        
        let color = model.isSelected ? UIColor.wmc_mainTheme : UIColor.wmc_grayH3_505050
        addressTitleLabel.textColor = color

        if model.style == .normal {
            setCommonly(false)
        } else if model.style == .commonly {
            setCommonly(true)
        } else if model.style == .customized {
            setCustomized()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    // Private

    private var index: Int = 0
    private var model: WmcSelectAddressModel?
    private var addressTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH3_505050
        label.font = 16.wmc_font
        return label
    }()

    private var addressDetailLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH12_A0A0A0
        label.font = 12.wmc_font
        return label
    }()

    private var commonlyIconView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH16_EBEBEB
        view.layer.cornerRadius = 2
        return view
    }()

    private var commonlyLabel: UILabel = {
        let label = UILabel()
        label.text = "常用地址"
        label.textColor = .wmc_grayH4_5E6063
        label.font = 10.wmc_font
        return label
    }()

    private var commonlyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("设为常用", for: .normal)
        btn.setTitle("取消常用", for: .selected)
        btn.setTitleColor(.wmc_mainTheme, for: .normal)
        btn.setTitleColor(.wmc_grayH9_8D8D8D, for: .selected)
        btn.titleLabel?.font = 12.wmc_font
        btn.layer.borderColor = UIColor.wmc_mainTheme.cgColor
        btn.layer.borderWidth = 1
        btn.layer.cornerRadius = 16
        return btn
    }()

    private var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH20_F7F7F7
        return view
    }()

    private func setCommonly(_ isCommonly: Bool) {
        let title: String = isCommonly ? "取消常用" : "设为常用"
        commonlyBtn.setTitle(title, for: .normal)
        
        let color: UIColor = isCommonly ? .wmc_grayH9_8D8D8D : .wmc_mainTheme
        commonlyBtn.setTitleColor(color, for: .normal)
        commonlyBtn.layer.borderColor = color.cgColor

        commonlyLabel.text = isCommonly ? "常用地址" : nil
        commonlyIconView.isHidden = !isCommonly
        let left = isCommonly ? 64 : 16
        
        commonlyIconView.snp.updateConstraints { make in
            make.width.equalTo(46)
        }
        
        addressDetailLabel.snp.updateConstraints { make in
            make.left.equalTo(left)
        }
    }
    
    private func setCustomized() {
        commonlyBtn.setTitle("删除", for: .normal)
        commonlyBtn.setTitleColor(.wmc_grayH9_8D8D8D, for: .normal)
        commonlyBtn.layer.borderColor = UIColor.wmc_grayH9_8D8D8D.cgColor
        
        commonlyIconView.isHidden = false
        
        commonlyLabel.text = "自定义地点"
        
        commonlyIconView.snp.updateConstraints { make in
            make.width.equalTo(58)
        }
        
        addressDetailLabel.snp.updateConstraints { make in
            make.left.equalTo(76)
        }
    }

    @objc
    private func commonlyAction(_ sender: UIButton) {
        
        guard let poi = model?.poi else { return }
        
        if model?.style == .normal {
            WmcCommonlyAddressDBManager.insert(model: poi) { [weak self] isSuccess in
                guard let weakSelf = self else { return }
                if isSuccess {
                    WmcLocationManager.setCommonlyAddress(poi: poi)
                    weakSelf.setCommonly(true)
                    weakSelf.setCommonlyAddress?(true, weakSelf.index)
                }
            }
        } else if model?.style == .commonly {
            WmcCommonlyAddressDBManager.delete(model: poi) { [weak self] isSuccess in
                guard let weakSelf = self else { return }
                if isSuccess {
                    WmcLocationManager.setCommonlyAddress(poi: poi)
                    weakSelf.setCommonly(false)
                    weakSelf.setCommonlyAddress?(false, weakSelf.index)
                }
            }
        } else if model?.style == .customized {
            WmcCustomAddressDBManager.delete(model: poi) { [weak self] isSuccess in
                guard let weakSelf = self else { return }
                if isSuccess {
                    WmcHudManager.toast("删除成功")
                    weakSelf.delCustomizedAddress?(weakSelf.index)
                }
            }
        }
    }
}
